/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.recycling.mq;

import com.medusa.gruul.order.api.model.OrderVo;
import com.medusa.gruul.recycling.constant.MqQueueNameConstant;
import com.medusa.gruul.recycling.service.IRecyclingAssOrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author whh
 * @description 用户队列监听
 * @data: 2019/12/11
 */
@Slf4j
@Component
public class RecyclingListener {

    @Autowired
    private IRecyclingAssOrderService recyclingAssOrderService;


    @RabbitListener(queues = MqQueueNameConstant.ORDER_PAY_OK_QUEUE_CHANGE)
    public void orderPayOk(OrderVo orderVo, MessageProperties properties, Channel channel) {
        log.info(" orderPayOk  receive message:  orderId = {}", orderVo.getId());
        //手动确认ack
        try {
            log.info("order productTotalQuantity number = {}",orderVo.getProductTotalQuantity());
            log.info("order status message = {}",orderVo.getStatus());
            recyclingAssOrderService.orderPayOk(orderVo);
            channel.basicAck(properties.getDeliveryTag(), true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @RabbitListener(queues = MqQueueNameConstant.ORDER_RETURN_QUEUE_CHANGE)
    public void refundNotify(OrderVo orderVo, MessageProperties properties, Channel channel) throws IOException {
        log.info(" refundNotify  receive message:  orderId = {}", orderVo.getId());
        try {
            log.info("order productTotalQuantity number = {}",orderVo.getProductTotalQuantity());
            log.info("order status message = {}",orderVo.getStatus());
            recyclingAssOrderService.refundNotify(orderVo);
            channel.basicAck(properties.getDeliveryTag(), true);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}
